package Window;

import dao.Impl.UserDaoImpl;
import entity.User;

import javax.swing.*;

public class Login extends JFrame {
    public Login(){
        init();
    }
//    public static void main(String[] args) {
//        Login login=new Login();
//        login.init();
//    }
    /*登录界面*/
    public  void init(){
        //创建窗口对象
        JFrame jFrame =new JFrame();

        jFrame.setSize(400,500);

        jFrame.setTitle("这是标题");
        jFrame.setDefaultCloseOperation(3);
        jFrame.setLocationRelativeTo(null);
        jFrame.setLayout(null);//设置布局为空


        JLabel nameStr= new JLabel("账号：");
        //设置位置
        nameStr.setBounds(80,70,50,30);
        jFrame.add(nameStr);


        JLabel paswoedStr= new JLabel("密码：");
        paswoedStr.setBounds(80,110,50,30);
        jFrame.add(paswoedStr);

        //文本框
        JTextField nameID=new JTextField();
        nameID.setBounds(120,70,150,30);
        jFrame.add(nameID);

        //密码文本框
        JPasswordField passwordStr=new JPasswordField();
         passwordStr.setBounds(120,110,150,30);
        jFrame.add(passwordStr);


        JButton jButton1=new JButton("登陆");
        jButton1.setBounds(60,190,100,30);
        jFrame.add(jButton1);
        jButton1.addActionListener(e -> {
            UserDaoImpl userDao=new UserDaoImpl();
            String inputID=new String(nameID.getText());
            String inputPass=new String(passwordStr.getText());
            User user=userDao.Login(new User(inputID,inputPass));
            //System.out.println(user.getId());
            if(user.getId()!=0){
                JOptionPane.showMessageDialog(null,"欢迎回来"+user.getName(),"登陆成功",-1);
                jFrame.setVisible(false);
                new Game();
            }else{
              JOptionPane.showMessageDialog(null,"账号密码输入有误!","错误",2);
            }
        });

        JButton jButton2=new JButton("注册");
          jButton2.setBounds(190,190,100,30);
          jFrame.add(jButton2);
          jButton2.addActionListener(e -> {
                jFrame.setVisible(false);
                new Register();
          });

        jFrame.setVisible(true);//显示控件
       // JButton jButton2=new JButton();

    }
}
